package com.wh.wisdomsite.xbox.st.service.impl;

import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wh.wisdomsite.xbox.ba.dao.SysSequenceMapper;
import com.wh.wisdomsite.xbox.core.XboxException;
import com.wh.wisdomsite.xbox.st.dao.StoreOutRecordMapper;
import com.wh.wisdomsite.xbox.st.entity.StoreOutRecord;
import com.wh.wisdomsite.xbox.st.service.IPurchaseApplyService;
import com.wh.wisdomsite.xbox.st.service.IStoreInRecordService;
import com.wh.wisdomsite.xbox.st.service.IStoreOutRecordService;
import com.wh.wisdomsite.xbox.utils.Const;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 出库记录实现类
 * @package com.wh.wisdomsite.xbox.st.service.impl
 * @author 蔡道行
 * @version V1.0
 * @date 15:05 2018/12/24
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 */
@Service("storeOutRecordService")
@Transactional(rollbackFor = {Exception.class, RuntimeException.class})
public class StoreOutRecordServiceImpl extends ServiceImpl<StoreOutRecordMapper,StoreOutRecord> implements IStoreOutRecordService {

    private static final Log logger = LogFactory.getLog(ProductServiceImpl.class);

    @Autowired
    private StoreOutRecordMapper storeOutRecordMapper;

    @Autowired
    private IStoreInRecordService iStoreInRecordService;

    @Autowired
    private SysSequenceMapper sysSequenceMapper;

    @Autowired
    private IPurchaseApplyService iPurchaseApplyService;

    @Override
    public Map<String, Object> getMapById(Serializable id) {
        return storeOutRecordMapper.getMapById(id);
    }

    @Override
    public Page<Map<String, Object>> getMapsPage(Page page, Wrapper<StoreOutRecord> wrapper) {
        SqlHelper.fillWrapper(page,wrapper);
        page.setRecords(this.baseMapper.getMapsPage(page,wrapper));
        return page;
    }

    @Override
    public Page<Map<String, Object>> getRecordPage(Page page, Wrapper<StoreOutRecord> wrapper) {
        SqlHelper.fillWrapper(page,wrapper);
        page.setRecords(this.baseMapper.getRecordPage(page,wrapper));
        return page;
    }

    @Override
    public String getStoCode(String coCode) throws Exception {
        //出库单号
        String stoCodeResult = "";
        //出库单前缀
        String ckd = "CKD";
        //出库单物料初始出库记录值
        String curRecord = "0001";
        //出库单号出库次数序列值
        Map<String,Object> parmaMap = new HashMap<>();
        parmaMap.put("seqName",Const.STOCODE_SEQ);
        parmaMap.put("seqValue",null);
        sysSequenceMapper.getSeqNextVal(parmaMap);
        Object valSeq = parmaMap.get("seqValue");
        if(valSeq == null){
            throw new XboxException("出库单号序列为空，保存失败");
        }
        String valSeqStr = String.valueOf(valSeq);
        //计算序列长度，依据长度值给定前缀
        String seqNumLen = String.valueOf(valSeqStr.length());
        String numberFront = iPurchaseApplyService.getNumberFront(seqNumLen);
        //当前出库次数
        String curTime = numberFront + valSeqStr;
        stoCodeResult = ckd + "_" + coCode + "_" +  curTime + "_" +  curRecord;
        return stoCodeResult;
    }
}
